import request from '../../../components/Request';

/**
 * 查询用户类型字典
 * @returns {Promise<*>}
 */
export async function listUserType() {
  let result = await request.post({ url: './dict/getDict.do', data: { key: 'userType' } });
  return result.data;
}

export async function listUserByOrgId(pageNo, pageSize, orgId) {
  let result = await request.post({
    url: './user/listUserByOrgId.do',
    data: { pageNo: pageNo, pageSize: pageSize, orgId: orgId },
  });
  return result.data;
}

export async function listUserByDeptId(pageNo, pageSize, deptId) {
  let result = await request.post({
    url: './user/listUserByDeptId.do',
    data: { pageNo: pageNo, pageSize: pageSize, deptId: deptId },
  });
  return result.data;
}

export async function listUserByName(pageNo, pageSize, orgId, name) {
  let result = await request.post({
    url: './user/listUserByName.do',
    data: { pageNo: pageNo, pageSize: pageSize, orgId: orgId, name: name },
  });
  return result.data;
}

export async function listUserRole(uid) {
  let result = await request.post({ url: './user/listUserRole.do', data: { uid: uid } });
  return result.data;
}

export async function listRole() {
  let result = await request.post({ url: './user/listRole.do' });
  return result.data;
}

export async function saveUser(user, roles) {
  let rolesStr = '';
  roles.forEach((item, index) => {
    rolesStr += ',' + item.id;
  });
  rolesStr = rolesStr.substring(1, rolesStr.length);
  user.roles = rolesStr;
  let result = await request.post({ url: './user/saveUser.do', data: user });
  return result;
}

export async function deleteUser(uid) {
  let result = await request.post({ url: './user/deleteUser.do', data: { uid: uid } });
}

export async function resetPassword(uid, password) {
  let result = await request.post({ url: './user/resetPassword.do', data: { uid: uid, password: password } });
}

export async function pinyin(name) {
  let result = await request.post({ url: './user/pinyin.do', data: { userName: name } });
  return result.data;
}
